function operationFormat(cellValue, options, rowObject) {
	return '<div class="operating" data-id="' + options.rowId + 
	'"><span id="opp" class="ui-icon ui-icon-plus" title="编辑" data-toggle="modal" data-target="#myModal"></span>&nbsp&nbsp';
}
function initGrid() {
	var col = [{
		name: "operate",
		index: "operate",
		label: "操作",
		width: 100,
		fixed: true,
		align: "center",
		formatter: Public.operFmatter
	},
	{
		name: "id",
		index: "id",
		label: "id",
		align : "center",
		width: 60
		/*hidden: true*/
	}, {
		name: "userCode",
		index: "userCode",
		label: "用户编码",
		align : "center",
		width: 100
	}, 
	{
		name: "userName",
		index: "userName",
		label: "用户名",
		align : "center",
		width: 100
	}, 
	 {
		name: "password",
		index: "password",
		align : "center",
		label: "密码",
		width: 100
	}, 
	{
		name: "CompanyName",
		index: "CompanyName",
		align : "center",
		label: "公司名称",
		width: 100
	}, 
	 {
		name: "DeptName",
		index: "DeptName",
		align : "center",
		label: "部门名称",
		width: 100
	}, 
	{
		name: "companyId",
		index: "companyId",
		align : "center",
		label: "公司id",
		/*hidden: true,*/
		width: 60
	}, 
	 {
		name: "deptId",
		index: "deptId",
		align : "center",
		label: "部门id",
		/*hidden: true,*/
		width: 60
	},
	{
		name: "tel",
		index: "tel",
		label: "tel",
		align : "center",
		width: 100
	}, 
	 {
		name: "mobile",
		index: "mobile",
		align : "center",
		label: "mobile",
		width: 100
	}, 
	 {
		name: "fax",
		index: "fax",
		label: "fax",
		align : "center",
		width: 100
	}, {
		name: "email",
		index: "email",
		label: "email",
		align : "center",
		width: 100
	},
	{
		name: "qq",
		index: "qq",
		label: "qq",
		align : "center",
		width: 100
	},
	{
		name: "weiXin",
		index: "weiXin",
		label: "weiXin",
		align : "center",
		width: 100
	},
	{
		name: "RoleName",
		index: "RoleName",
		label: "角色名",
		align : "center",
		width: 100
	},
	{
		name: "RID",
		index: "RID",
		label: "角色id",
		align : "center",
		hidden: true,
		width: 100
	}];
	$("#grid").jqGrid({
		url: "userAll.do",
		datatype: "json",
		height: 350,
		altRows: true,
		gridview: true,
		colModel: col,
		autowidth: true,
		viewrecords: true,
		cmTemplate: {
			sortable: false,
			title: false
		},
		page: 1,
		pager: "#page",
		rowNum: 10,
		shrinkToFit: false,
		scroll: 1, 
		rowList:[10,20,30],
		jsonReader: {
			root: "data.items",
			records: "data.records",
			repeatitems: false,
			id: "id"
		},
		loadComplete: function(data) {
			console.log(data);
			if (data && 200 == data.status) {
				var list = {};
				data = data.data;
				for (var i = 0; i < data.items.length; i++) {
					var data_item = data.items[i];
					list[data_item.id] = data_item;
				}
			} else {
				var msg = 250 == data.status ? "没有用户数据！" : "获取用户数据失败！" + data.msg;
				parent.Public.tips({
					type: 2,
					content: msg
				});
			}
		},
		loadError: function() {
			parent.Public.tips({
				type: 1,
				content: "操作失败了哦，请检查您的网络链接！"
			})
		}
	});
}

handle = {
		operate: function(oper, rowid) {
			var title;
			if ("add" == oper) {
				title = "新增用户";
				({
					oper: oper,
					callback: this.callback
				})
			} else {
				title = "修改用户";
				({
					oper: oper,
					rowData: $("#grid").jqGrid('getRowData',rowid),
					callback: this.callback
				})
			}
			var cont = ['<div>',
			'<form id="manage-form" action="">', 
				'<ul class="mod-form-rows manage-wrap base-form" id="manager">', 
				'<li class="row-item">',
					'<div class="label-wrap"><label for="userCode">用户编码:</label></div>', 
					'<div class="ctn-wrap" style=""><input type="text" value="" class="ui-input" name="userCode" id="userCode" onblur="codeCheck()"><label id="userCode-error" class="valid-error" for="userCode" style="visibility:hidden;">用户编号不能为空</label></div>',
				"</li>",
				'<li class="row-item">', 
					'<div class="label-wrap"><label for="userName">&nbsp;用户名:</label></div>', 
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="userName" id="userName" onblur="nameCheck()"><label id="userName-error" class="valid-error" for="userName" style="visibility:hidden;">用户名不能为空</label></div>', 
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="password">&nbsp;&nbsp;&nbsp;密码:</label></div>',
					'<div class="ctn-wrap"><input type="password" value="" class="ui-input" name="password" id="password" onblur="passwordCheck()"><label id="password-error" class="valid-error" for="password" style="visibility:hidden;">密码不能为空</label></div>',
				"</li>",
				' <li class="row-item">',
				'<div class="label-wrap"><label for="CompanyName">&nbsp;公司名称:</label></div>',
				'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="CompanyName" id="CompanyName" onblur="CompanyNameCheck()"><label id="CompanyName-error" class="valid-error" for="CompanyName" style="visibility:hidden;">公司名称不能为空</label></div>',
			"</li>",
			' <li class="row-item">',
				'<div class="label-wrap"><label for="DeptName">部门名称:</label></div>',
				'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="DeptName" id="DeptName" onblur="DeptNameCheck()"><label id="DeptName-error" class="valid-error" for="DeptName" style="visibility:hidden;">部门名称不能为空</label></div>',
			"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="companyId">&nbsp;公司id:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="companyId" id="companyId" onblur="companyIdCheck()"><label id="companyId-error" class="valid-error" for="companyId" style="visibility:hidden;">公司id不能为空</label></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="deptId">部门id:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="deptId" id="deptId" onblur="deptIdCheck()"><label id="deptId-error" class="valid-error" for="deptId" style="visibility:hidden;">部门id不能为空</label></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="tel">&nbsp;&nbsp;&nbsp;tel:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="tel" id="tel" onblur="telephoneCheck()"><label id="tel-error" class="valid-error" for="tel" style="visibility:hidden;">请填写正确的座机号码</label></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="mobile">mobile:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="mobile" id="mobile" onblur="phoneCheck()"><label id="mobile-error" class="valid-error" for="mobile" style="visibility:hidden;">请填写正确的手机号码</label></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="cardNo">email:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="email" id="email" onblur="checkEmail()"><label id="email-error" class="valid-error" for="email" style="visibility:hidden;">请填写正确的邮箱格式</label></div>',
				"</li>",
				' <li class="row-item" >',
					'<div class="label-wrap"><label for="cardNo">&nbsp;&nbsp;&nbsp;fax:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="fax" id="fax"></div>',
				"</li>",
				
				' <li class="row-item">',
					'<div class="label-wrap"><label for="cardNo">&nbsp;&nbsp;&nbsp;qq:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="qq" id="qq"></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="weiXin">&nbsp;&nbsp;&nbsp;微信:</label></div>',
					'<div class="ctn-wrap"><input type="text" value="" class="ui-input" name="weiXin" id="weiXin"></div>',
				"</li>",
				' <li class="row-item">',
					'<div class="label-wrap"><label for="rolename">&nbsp;&nbsp;&nbsp;角色名:</label></div>',
					"<div class='ctn-wrap'><select class='demo' id ='rolename' name='rolename'></select></div>",
				"</li>",
				' <li class="row-item" style="visibility:hidden;">',
					'<div class="label-wrap"><label for="RID">&nbsp;&nbsp;&nbsp;角色id:</label></div>',
					"<div class='ctn-wrap'><input type='text' value='' class='ui-input' name='RID' id='RID'></div>",
				"</li>",
				/*' <li class="row-item">',
					'<div class="label-wrap"><label for="RID">&nbsp;&nbsp;&nbsp;角色编码:</label></div>',
					"<div class='ctn-wrap'><input type='text' value=',+${listRole.rolecode}+,' class='ui-input' name='rolecode' id='rolecode'></div>",
				"</li>",*/
			"</ul>", 
	   "</form>",'</div>'];
			this.dialog = $.dialog({
				title: title,
				content: cont.join(""),
				width: 600,
				height: 90,
				max: false,
				min: false,
				cache: false,
				lock: true,
				okVal: "确定",
				ok: function() {
					
					 if(($("#userCode").val()=='')&& ($("#userName").val()=='') && ($("#password").val()=='') && ($("#companyId").val()=='') && ($("#deptId").val()=='')){
						 $("#userCode-error").css("visibility","visible");
							$("#userName-error").css("visibility","visible");
							$("#password-error").css("visibility","visible");
							$("#companyId-error").css("visibility","visible");
							$("#deptId-error").css("visibility","visible");
							$("#userCode").addClass("valid-error");
							$("#userName").addClass("valid-error");
							$("#password").addClass("valid-error");
							$("#companyId").addClass("valid-error");
							$("#deptId").addClass("valid-error");
						}else if($("#userCode").val()==''){
							$("#userCode-error").css("visibility","visible");
					}else if($("#userName").val()==''){
						$("#userName-error").css("visibility","visible");
					}else if($("#password").val()==''){
						$("#password-error").css("visibility","visible");
					}else if($("#companyId").val()==''){
						$("#companyId-error").css("visibility","visible");
					}else if($("#deptId").val()==''){
						$("#deptId-error").css("visibility","visible");
					}else{
						var RID = $("#RID").val();
						/*alert(RID)*/
						postData(rowid);
					}
					return false;
					
				},
				cancelVal: "取消",
				cancel: function() {
					return true
				},
				init: function() {
					if("add" != oper) {
						var rowdata = $("#grid").jqGrid('getRowData',rowid);
						$("#userCode").val(rowdata.userCode);
						$("#userName").val(rowdata.userName);
						$("#password").val(rowdata.password);
						$("#companyId").val(rowdata.companyId);
						$("#deptId").val(rowdata.deptId);
						$("#tel").val(rowdata.tel);
						$("#mobile").val(rowdata.mobile);
						$("#fax").val(rowdata.fax);
						$("#email").val(rowdata.email);
						$("#qq").val(rowdata.qq);
						$("#weiXin").val(rowdata.weiXin);
						$("#RID").val(rowdata.RID);
					}
				}
			})
		},
		del: function(rowId) {
			alert(rowId)
			debugger
			$.dialog.confirm("删除的用户信息将不能恢复，请确认是否删除？", function() {
				debugger
				Public.ajaxPost("userdelete.do", {
					id: rowId
				}, function(ret) {
					if (ret && 200 == ret.status) {
						parent.Public.tips({
							content: "删除用户信息成功！"
						}), $("#grid").jqGrid("delRowData", rowId);
					} else parent.Public.tips({
						type: 1,
						content: "删除用户信息失败！" + ret.msg
					})
				})
			})
		},
		callback: function(data, oper) {
			var griddata = $("#grid").data("gridData");
			griddata || (griddata = {}, $("#grid").data("gridData", griddata));
			griddata[data.id] = data;
			"add" != oper ? ($("#grid").jqGrid("setRowData", data.id, data), this.dialog.close()) : ($("#grid").jqGrid("addRowData", data.id, data, "last"), this.dialog.close()), $("#grid").setGridParam({
				postData: data
			}).trigger("reloadGrid");
		}
};


function getRoleName(){
	$.ajax({ 
		type: "post", 
		url: "findRoleAll.do",
		dataType: "json", 
		async: false,
		success: function (data) {
			/*var theOption ="<option value='' selected='selected'  >角色名称</option>";*/
			var theOption = "";
			$.each(data.listRole,function(index,listRole){
				theOption +="<option value='"+listRole.rolename+"'>"+listRole.rolename+"</option>";
			});
			/*demoid + ="<input type='text' value='"+${listRole.id}+"' class='ui-input' name='RID' id='RID'>";*/
			$(".demo").html(theOption);
		}
	});
}


function initEvent() {
	$("#btn-add").click(function(a) {
		
		a.preventDefault();
		handle.operate("add")
		//添加
		getRoleName();
		
		
	}), $("#grid").on("click", ".operating .ui-icon-pencil", function(a) {
		a.preventDefault();
		var rowid = $(this).parent().data("id");
		/*var RID = $(this).parent().parent()
		alert(RID);*/
		console.log(rowid);
		handle.operate("edit", rowid)
		//
		getRoleName();
		
		
	}),
	$("#grid").on("click", ".operating .ui-icon-trash", function(a) {
		a.preventDefault();
		var rowdata = $(this).parent().data("id");
		handle.del(rowdata)
	}),
	//用户角色的选择
	$("#grid").on("click", ".operating .ui-icon-plus", function(a) {
		a.preventDefault();
		var rowdata = $(this).parent().data("id");
		alert(rowdata)
	}),
	$("#btn-refresh").click(function(a) {
		a.preventDefault(), $("#grid").trigger("reloadGrid")
	}),
	 $_matchCon = $("#matchCon"),
	    $_matchCon.focus(function(){
	        if($.trim(this.value) == this.defaultValue){
	            this.value = '';
	          }
	          $(this).removeClass('ui-input-ph');
	        }).blur(function(){
	          var val = $.trim(this.value);
	          if(val == '' || val == this.defaultValue){
	            $(this).addClass('ui-input-ph');
	          }
	          val == '' && $(this).val(this.defaultValue);
	        }),
	    $("#search").on("click",
		    function(e) {
		        e.preventDefault();
		        var a = $_matchCon.val() === $_matchCon[0].defaultValue ? "": $.trim($_matchCon.val());
		        console.info(a)
		        t = 1,
		        $("#grid").jqGrid("setGridParam", {
		            page: 1,
		            postData: {
		                skey: a,
		            }
		        }).trigger("reloadGrid"),
		        t = 0
		    })
}

function codeCheck(){
	var userCode = $("#userCode").val();
	if(userCode==''){
		//alert("用户编码不能为空")
		 $("#userCode-error").css("visibility","visible");
			/*$("#userName-valid-error").css("visibility","visible");*/
		 $("#userCode").addClass("valid-error");
	}else{
		var b = /^[a-zA-Z0-9\-_]*$/;
		if(!b.test(userCode)){
			$().append("<span>")
			alert("编号只能由数字、字母、-或_等字符组成");
			return false;
		}
		$("#userCode-error").css("visibility","hidden");
		$("#userCode").removeClass("valid-error");
	}
}
function nameCheck(){
	var userName = $("#userName").val();
	if(userName==''){
		 $("#userName-error").css("visibility","visible");
			$("#userName").addClass("valid-error");
			
	}else{
		$("#userName-error").css("visibility","hidden");
		$("#userName").removeClass("valid-error");
		
	}
}
function passwordCheck(){
	var password = $("#password").val();
	if(password==''){
		 $("#password-error").css("visibility","visible");
			$("#password").addClass("valid-error");
	}else{
		$("#password-error").css("visibility","hidden");
		$("#password").removeClass("valid-error");
		
	}
}
function companyIdCheck(){
	var companyId = $("#companyId").val();
	if(companyId==''){
		 $("#companyId-error").css("visibility","visible");
		 $("#companyId").addClass("valid-error");
			
	}else{
		$("#companyId-error").css("visibility","hidden");
		 $("#companyId").removeClass("valid-error");
	}
}
function deptIdCheck(){
	var deptId = $("#deptId").val();
	if(deptId==''){
		//alert("用户编码不能为空")
		 $("#deptId-error").css("visibility","visible");
		 $("#deptId").addClass("valid-error");
			/*$("#userName-valid-error").css("visibility","visible");*/
	}else{
		$("#deptId-error").css("visibility","hidden");
		 $("#deptId").removeClass("valid-error");
	}
}
function CompanyNameCheck(){
	var CompanyName = $("#CompanyName").val();
	if(CompanyName==''){
		 $("#CompanyName-error").css("visibility","visible");
		 $("#CompanyName").addClass("valid-error");
			/*$("#userName-valid-error").css("visibility","visible");*/
	}else{
		$("#CompanyName-error").css("visibility","hidden");
		 $("#CompanyName").removeClass("valid-error");
	}
}
function DeptNameCheck(){
	var DeptName = $("#DeptName").val();
	if(DeptName==''){
		 $("#DeptName-error").css("visibility","visible");
		 $("#DeptName").addClass("valid-error");
			/*$("#userName-valid-error").css("visibility","visible");*/
	}else{
		$("#DeptName-error").css("visibility","hidden");
		 $("#DeptName").removeClass("valid-error");
	}
}


function phoneCheck() {
	var mobile = $("#mobile").val();
	var b = /^1[34578]\d{9}$/;
	if(!b.test(mobile)){
		/*alert("手机号码有误，请填写正确的手机号码");*/
		$("#mobile-error").css("visibility","visible");
		$("#mobile").removeClass("valid-error");
		return false;
	}else{
		$("#mobile-error").css("visibility","hidden");
		$("#mobile").removeClass("valid-error");
	}
	/*return b.test(a) ? [!0, ""] : [!1, "请填写正确的手机号码"]*/
}
function telephoneCheck() {
	var tel = $("#tel").val();
	var b = /^([\d]+-){1,2}[\d]+$/;
	if(!b.test(tel)){
		/*alert("请填写正确的座机号码，格式为0754-1234567或086-0754-1234567")*/
		$("#tel-error").css("visibility","visible");
		$("#tel").addClass("valid-error");
		return false;
	}else{
		$("#tel-error").css("visibility","hidden");
		$("#tel").removeClass("valid-error");
	}
}
function checkEmail(){
	var email = $("#email").val();
	var re=/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(!re.test(email)){
		/*alert("邮箱格式错误");*/
		$("#email-error").css("visibility","visible");
		$("#email").addClass("valid-error");
	}else{
		$("#email-error").css("visibility","hidden");
		$("#email").removeClass("valid-error");
	}
}

function postData(data) {

	var oper = data ? "update" : "add";

	var post = {
			userCode: $.trim($("#userCode").val()),
			id: data,
			userName: $.trim($("#userName").val()),
			password: $.trim($("#password").val()),
			companyId: $.trim($("#companyId").val()),
			deptId: $.trim($("#deptId").val()),
			tel: $.trim($("#tel").val()),
			mobile: $.trim($("#mobile").val()),
			fax: $.trim($("#fax").val()),
			email: $.trim($("#email").val()),
			qq: $.trim($("#qq").val()),
			weiXin: $.trim($("#weiXin").val()),
			rolename:$.trim($("#rolename").val()),
			RID:$.trim($("#RID").val())
			
	},
	msg = "add" == oper ? "新增用户信息" : "修改用户信息";

	Public.ajaxPost("user" + oper + ".do", post, function(retdata) {
		200 == retdata.status ? (parent.parent.Public.tips({
			content: msg + "成功！"
		}), handle.callback(retdata.data, oper)) : parent.parent.Public.tips({
			type: 1,
			content: msg + "失败！" + retdata.msg
		})
	})
}

/*customerCombo($("#id"), {
	defaultSelected : 0,
	data : function(){
		return '/url';
	},
	addOptions : {
		text : "(空)",
		value : 0
	}
});*/

initGrid();
initEvent();